﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.Blog.Services.Menus
{
    public static class MenuFinder
    {
        public static IEnumerable<MenuChildren> GetBlogMenuChildren(IEnumerable<MenuDto> _menus)
        {
            foreach (var item in _menus.Where(c => c.ParentId == null))
            {
                var menu = new MenuChildren(item.Id, item.Title, item.Path);
                var children = menu.CreateChildrenMenu(item.Id, _menus);
                yield return children;
            }
        }
        public static MenuChildren CreateChildrenMenu(this MenuChildren menu, int? parentId, IEnumerable<MenuDto> _menus)
        {
            foreach (var item in _menus.Where(c => c.ParentId == parentId))
            {
                var children = menu.CreateChildren(item.Id, item.Title, item.Path);
                if (_menus.Any(c => c.ParentId == item.Id))
                    children.CreateChildrenMenu(item.Id, _menus);
            }
            return menu;
        }
    }
}
